package com.app.car.bean;


import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.annotation.JSONField;
import com.app.car.basis.Constants;
import com.app.car.jpush.TagAliasOperatorHelper;

import java.util.ArrayList;
import java.util.List;

import my.FileUtils;
import my.LogUtil;
import my.MySharedPreferences;


public class LoginBean extends BasisBean {
    private static final long serialVersionUID = 1L;

    public static final int ERRO_CODE_DEVICE = -3001;


    public static final int LOGIN_TYPE_PERSON = 0;
    public static final int LOGIN_TYPE_FA = 1;

    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "login.data";
    private static LoginBean sLoginBean;




    @JSONField(name = "uid")
    private String uid;
    @JSONField(name = "uno")
    private String uno;
    @JSONField(name = "uname")
    private String uname;
    @JSONField(name = "aid")
    private String aid;
    @JSONField(name = "aname")
    private String aname;
    @JSONField(name = "bname")
    private String bname;
    @JSONField(name = "token")
    private String token;

    @JSONField(name = "right")
    private List<MenuBean> right = new ArrayList<>();


    public LoginBean() {
        super();
        // sLoginBean = this;
    }

    public static boolean isLogin() {
		return getInstance() != null && !TextUtils.isEmpty(getInstance().getToken());
    }

    public static LoginBean getInstance() {
        if (sLoginBean == null)
            sLoginBean = getFromCache();
        return sLoginBean;
    }

    public static LoginBean getFromCache() {
        LoginBean data;
        data = (LoginBean) FileUtils.readObject(FILE_CACHE);
        if (data != null) {
            sLoginBean = data;
        }else {
            data = new LoginBean();
        }
        LogUtil.i("login---->getFromCache---" + (data == null));
        return data;
    }

    public void save() {
        sLoginBean = this;
        FileUtils.cacheObject(FILE_CACHE, this);
        // FileSaveHandler.saveObject(FILE_CACHE, this);
        LogUtil.i("login---->save---" + FILE_CACHE);
    }

    private static void delete() {
        FileUtils.cacheObject(FILE_CACHE, null);
        // FileSaveHandler.removeObject(FILE_CACHE);
    }

	public static void logout() {
		delete();
		sLoginBean = null;
//        TagAliasOperatorHelper.getInstance().cleanAlias();
	}

    public static void logout(Context mContext) {
        delete();
        sLoginBean = null;
        MySharedPreferences mSp = new MySharedPreferences(mContext);
        mSp.putIsLogined(false);
        mSp.putPassword("");
        mSp.putUser("");
        TagAliasOperatorHelper.getInstance().cleanAlias();
    }

    public static void logPast(Context mContext) {
        delete();
        sLoginBean = null;
        MySharedPreferences mSp = new MySharedPreferences(mContext);
        mSp.putIsLogined(false);
    }

    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }


//    public   String getJpushAlias(){
//        return "ukluser_"+getUserId();
//    }


    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }


    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getUno() {
        return uno;
    }

    public void setUno(String uno) {
        this.uno = uno;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getAid() {
        return aid;
    }

    public void setAid(String aid) {
        this.aid = aid;
    }

    public String getAname() {
        return aname;
    }

    public void setAname(String aname) {
        this.aname = aname;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public List<MenuBean> getRight() {
        return right;
    }

    public void setRight(List<MenuBean> right) {
        this.right = right;
    }

    public List<MenuBean> getRightTrue(){
        List<MenuBean> menuList = new ArrayList<>();
        for(MenuBean data : right){
            if( data.isSele()){
                menuList.add(data);
            }
        }
        return menuList;
    }



    public boolean canAddTeaMan(){
        boolean isCan = false;
        for (MenuBean menuBean : getInstance().getRight()){
            if (menuBean.getMna().contains("基础资料")){
               for(MenuBean child: menuBean.getChild()){
                   if(child.getSna().equals("供应商资料")&& child.isSele()){
                       isCan = true;
                   }
               }
            }
        }
        return isCan;
    }

}
